package com.xyy.controller;

import com.xyy.common.Result;
import com.xyy.dto.ActivityRegistrationDTO;
import com.xyy.service.ActivityRegistrationService;
import com.xyy.vo.ActivityRegistrationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 活动报名Controller
 */
@Slf4j
@RestController
@RequestMapping("/activity-registration")
public class ActivityRegistrationController {
    
    @Resource
    private ActivityRegistrationService registrationService;
    
    /**
     * 报名活动
     */
    @PostMapping("/register")
    public Result<ActivityRegistrationVO> registerActivity(@Valid @RequestBody ActivityRegistrationDTO registrationDTO,
                                                         HttpServletRequest request) {
        log.info("开始处理活动报名请求，registrationDTO: {}", registrationDTO);
        
        Long userId = (Long) request.getAttribute("userId");
        log.info("从request中获取的userId: {}", userId);
        
        if (userId == null) {
            log.warn("userId为null，用户未登录或登录信息无效");
            return Result.error("用户未登录或登录信息无效");
        }
        
        log.info("开始调用service层进行报名，userId: {}, activityId: {}, patientId: {}", 
                userId, registrationDTO.getActivityId(), registrationDTO.getPatientId());
        
        ActivityRegistrationVO result = registrationService.registerActivity(registrationDTO, userId);
        log.info("活动报名成功，result: {}", result);
        return Result.success("报名成功", result);
    }
    
    /**
     * 取消报名
     */
    @PostMapping("/{id}/cancel")
    public Result<Boolean> cancelRegistration(@PathVariable Long id, HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            if (userId == null) {
                return Result.error("用户未登录或登录信息无效");
            }
            
            boolean success = registrationService.cancelRegistration(id, userId);
            if (success) {
                return Result.success(true);
            } else {
                return Result.error("取消报名失败");
            }
        } catch (Exception e) {
            log.error("取消报名失败", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 确认报名（管理员操作）
     */
    @PostMapping("/{id}/confirm")
    public Result<Boolean> confirmRegistration(@PathVariable Long id, HttpServletRequest request) {
        try {
            Long adminId = (Long) request.getAttribute("userId");
            if (adminId == null) {
                return Result.error("管理员未登录或登录信息无效");
            }
            
            boolean success = registrationService.confirmRegistration(id, adminId);
            if (success) {
                return Result.success(true);
            } else {
                return Result.error("确认报名失败");
            }
        } catch (Exception e) {
            log.error("确认报名失败", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取用户的报名列表
     */
    @GetMapping("/user/list")
    public Result<List<ActivityRegistrationVO>> getUserRegistrations(HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            if (userId == null) {
                return Result.error("用户未登录或登录信息无效");
            }
            
            List<ActivityRegistrationVO> registrations = registrationService.getUserRegistrations(userId);
            return Result.success(registrations);
        } catch (Exception e) {
            log.error("获取用户报名列表失败", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取活动的报名列表（管理员查看）
     */
    @GetMapping("/activity/{activityId}/list")
    public Result<List<ActivityRegistrationVO>> getActivityRegistrations(@PathVariable Long activityId,
                                                                       HttpServletRequest request) {
        try {
            Long adminId = (Long) request.getAttribute("userId");
            if (adminId == null) {
                return Result.error("管理员未登录或登录信息无效");
            }
            
            List<ActivityRegistrationVO> registrations = registrationService.getActivityRegistrations(activityId, adminId);
            return Result.success(registrations);
        } catch (Exception e) {
            log.error("获取活动报名列表失败", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 检查是否已报名
     */
    @GetMapping("/check")
    public Result<Boolean> checkRegistration(@RequestParam Long activityId,
                                           @RequestParam Long patientId) {
        try {
            boolean isRegistered = registrationService.isAlreadyRegistered(activityId, patientId);
            return Result.success(isRegistered);
        } catch (Exception e) {
            log.error("检查报名状态失败", e);
            return Result.error(e.getMessage());
        }
    }
} 